package com.wbhz.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.wbhz.entity.User;
import com.wbhz.entity.Vip;
import com.wbhz.entity.BankCard;
import com.wbhz.entity.Message;
@Mapper
public interface UserMapper {
	@Insert("insert into t_user values(null,#{userEmail},#{userName},#{userPwd},#{userSex}, null, null, null, 100, null, now())")
	@Options(useGeneratedKeys=true)
	int insertUser(User user);
	
	@Insert("insert into t_right values(null, 1, (select user_id from t_user where user_name=#{userName}))")
	@Options(useGeneratedKeys=true)
	int insertRight(User user);
	
	@Select("select * from t_user where user_email=#{userEmail}")
	User selectUserByEmail(User user);
	
	@Select("select * from t_user where user_email=#{userEmail} and user_pwd=#{userPwd}")
	User selectUserByEmailAndPwd(User user);
	
	@Update("update t_user set user_name=#{userName}, user_sex=#{userSex}, user_city=#{userCity}, user_motto=#{userMotto} where user_email=#{userEmail}")
	int updateUserDetail(User user);
	
	@Update("update t_user set user_pwd=#{userPwd} where user_email=#{userEmail}")
	int updatePwd(@Param("userPwd")String userPwd, @Param("userEmail")String userEmail);
	
	@Update("update t_user set user_money=user_money - #{postReward} where user_id=#{userId}")
	int updateMoney(@Param("userId")int userId, @Param("postReward")int postReward);
	
	@Select("select user_id,user_name,post_stick from t_user,t_post where user_id=post_user_id order by post_id desc")
	List<User> selectUserIdAndName();
	
	@Select("select user_id,user_name,reply_postId,reply_status from t_user,t_reply where user_id=reply_fromReplyUserId and reply_postId=#{postId} order by reply_id desc")
	List<User> selectUserByReply(@Param("postId")int postId);
	
	@Insert("insert into t_bank_card values(null,(select user_id from t_user where user_name=#{userName}),#{random},'123456',1000)")
	@Options(useGeneratedKeys=true)
	int insertBankCard(@Param("userName")String userName, @Param("random")String random);
	
	@Select("select * from t_user u,t_message m "
			+ "where m.msg_byUserId=#{userId} "
			+ "and u.user_id=m.msg_fromUserId order by msg_id desc")
	List<Message> selectUserMsg(@Param("userId")int userId);
	
	@Select("select * from t_message where msg_id=#{msgId}")
	Message selectMsgDetail(@Param("msgId")int msgId);
	
	@Select("select user_id from t_user where user_name=#{byReplyName}")
	int selectUIdByUName(@Param("byReplyName")String byReplyName);
	
	@Select("select user_id,user_name from t_user,t_reply where reply_postId=#{postId} and reply_fromReplyUserId=user_id")
	List<User> selectReplyUList(@Param("postId")int postId);
	// 银行卡
	@Select("select * from t_bank_card where bank_card_user_id=#{userId}")
	BankCard selectBankCard(@Param("userId")int userId);
	@Select("select * from t_bank_card where bank_card_number=#{bankcardNumber} and bank_card_pwd=#{bankcardPwd}")
	Integer selectBanselectB_CByNumAndPwdkCard(@Param("bankcardNumber")String bankcardNumber, @Param("bankcardPwd")String bankcardPwd);
	@Update("update t_bank_card set bank_card_balance=bank_card_balance - #{balance} where bank_card_number=#{bankcardNumber}")
	int subB_Cbalance(@Param("bankcardNumber")String bankcardNumber, @Param("balance")int balance);
	
	@Select("select right_role_id from t_right where right_user_id=#{userId}")
	int selectURight(@Param("userId")int userId);
	// 使用银行卡金额充值会员
	@Update("update t_bank_card set bank_card_balance=bank_card_balance - #{balance} where bank_card_user_id=#{userId} and bank_card_balance >= #{balance}")
	void subB_CBalance2(@Param("balance")int balance, @Param("userId")int userId);
	// 飞吻充值会员
	@Update("update t_user set user_money=user_money - #{balance} where user_id=#{userId} and user_money >= 1000")
	void subUMoney(@Param("userId")int userId, @Param("balance")int balance);
	
	@Select("select * from t_user where user_money >= #{balance} and user_id=#{userId}")
	Integer isEnougMoney(@Param("userId")int userId, @Param("balance")int balance);
	@Select("select * from t_bank_card where bank_card_balance >= #{balance} and bank_card_user_id=#{userId}")
	Integer isEnougBalance(@Param("userId")int userId, @Param("balance")int balance);
	
	@Update("update t_right set right_role_id=2 where right_user_id=#{userId}")
	int updateURight(@Param("userId")int userId);
	@Insert("insert into t_vip values(null, #{userId}, now(), date_add(now(), interval 1 month) )")
	int insertVip1Month(@Param("userId")int userId);
	@Insert("insert into t_vip values(null, #{userId}, now(), date_add(now(), interval 3 month) )")
	int insertVip3Month(@Param("userId")int userId);
	@Insert("insert into t_vip values(null, #{userId}, now(), date_add(now(), interval 6 month) )")
	int insertVip6Month(@Param("userId")int userId);
	@Insert("insert into t_vip values(null, #{userId}, now(), date_add(now(), interval 12 month) )")
	int insertVip12Month(@Param("userId")int userId);
	
	@Select("select * from t_vip where vip_userId=#{userId}")
	Vip selectVipDetail(@Param("userId")int userId);
	
}
